package com.dq.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dq.eduservice.entity.EduSubject;
import com.dq.eduservice.entity.excel.SubjectData;
import com.dq.eduservice.entity.subject.Subject;
import com.dq.eduservice.entity.subject.VfpSubject;
import com.dq.eduservice.listener.SubjectListener;
import com.dq.eduservice.service.EduSubjectService;
import com.dq.eduservice.mapper.EduSubjectMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * 课程科目(EduSubject)表服务实现类
 *
 * @author Dq
 * @since 2022-07-21 15:40:39
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    @Autowired
    private EduSubjectMapper subjectMapper;

    //添加课程
    @Override
    public void addSubject(MultipartFile file, EduSubjectService subjectService) {
        /**
         * 读取excel
         * 1. 创建实体类对应关系
         * 2. 创建监听器 实现操作
         */
        try {
            InputStream fileStream = file.getInputStream();
            EasyExcel.read(fileStream, SubjectData.class, new SubjectListener(subjectService)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<Subject> getSubjectList() {
        /**
         * 0. 准备一个容器
         * 1. 查询所有一级
         * 2. 然后通过递归查询二级
         */
        List<Subject> subjectList = new ArrayList<Subject>();
        LambdaQueryWrapper<EduSubject> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EduSubject::getParentId, "0");
        List<EduSubject> eduSubjects = subjectMapper.selectList(queryWrapper);
        //subjet（一个一级一个二级数组）   <-  twoSubjectArrays（一个二级数组封装多个二级对象） <-  twoSubjet（二级对象）
        Subject subject = null;
        ArrayList<VfpSubject> twoSubjectArrays = null;
        VfpSubject twoSubject = null;
        //统一结果返回
        return getFinalSubjetsType(subjectList, eduSubjects);
    }

    /**
     * 1. 遍历所有一级路由
     * 2. 根据一级路由的id 遍历所有二级路由
     * 3. 一个一个封装到Subjec中去  然后最终封装到subjectList集合中去
     * 4. 最后返回
     * @param subjectList
     * @param eduSubjects
     * @return
     */
    private List<Subject> getFinalSubjetsType(List<Subject> subjectList, List<EduSubject> eduSubjects) {
        ArrayList<VfpSubject> twoSubjectArrays;
        VfpSubject twoSubject;
        Subject subject;
        for (EduSubject eduSubject : eduSubjects) {

            subject = new Subject(); //单个存入一二级的目录
            //存储一级id  title
            String parentId = eduSubject.getId();
            /*subject.setId(parentId);
            subject.setTitle(eduSubject.getTitle());*/
            BeanUtils.copyProperties(eduSubject,subject);
            //根据一级的id逐个遍历二级 然后遍历存入
            LambdaQueryWrapper<EduSubject> twoQueryWrapper = new LambdaQueryWrapper<>();
            twoQueryWrapper.eq(EduSubject::getParentId, parentId);
            List<EduSubject> twoEduSubjects = subjectMapper.selectList(twoQueryWrapper);
            //遍历2级所有 单个封装为twoSubject   然后存入twoSubjectArrays
            twoSubjectArrays = new ArrayList<>();
            for (EduSubject twoEduSubject : twoEduSubjects) {
                twoSubject = new VfpSubject();//单个存入二级
                /*twoSubject.setId(twoEduSubject.getId());
                twoSubject.setTitle(twoEduSubject.getTitle());*/
                BeanUtils.copyProperties(twoEduSubject,twoSubject);
                twoSubjectArrays.add(twoSubject); //二级容器 children
            }
            subject.setChildren(twoSubjectArrays);
            subjectList.add(subject);
        }

        return subjectList;
    }
}